//
//  RootViewController.swift
//  UIScrollViewDemo
//
//  Created by chengxiang on 2018/6/25.
//  Copyright © 2018年 chengxiang. All rights reserved.
//

import UIKit

class RootViewController: UIViewController,UIScrollViewDelegate,UIPageViewControllerDelegate {
    var scrollView = UIScrollView()
    var pageControl = UIPageControl()
    var isPageControlled = false
    var subViewCount:CGFloat = 3
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.green
        // Do any additional setup after loading the view.
        var screenFrame = UIScreen.main.bounds
        let screenWidth = screenFrame.width
        let screenHeight = screenFrame.height
        
        scrollView.frame = screenFrame
        scrollView.isPagingEnabled = true
        scrollView.contentSize = CGSize(width: screenWidth*subViewCount, height: screenHeight)
        scrollView.backgroundColor = UIColor.black
        scrollView.delegate = self
        
        let pcHeight:CGFloat = 50
        let pcRect = CGRect(x: 0, y: screenHeight-pcHeight, width: screenWidth, height: pcHeight)
        
        pageControl.frame = pcRect
        pageControl.numberOfPages = Int(subViewCount)
        pageControl.currentPage = 0
        pageControl.backgroundColor = UIColor.darkGray
        pageControl.addTarget(self, action: #selector(RootViewController.pageControllDidChanged(_:)), for: UIControlEvents.valueChanged)
        
        let firstController = FistSubViewController()
        screenFrame.origin.x = 0
        firstController.view.frame = screenFrame
        
        let secondController = SecondSubViewController()
        screenFrame.origin.x = screenFrame.size.width
        secondController.view.frame = screenFrame
        
        let thirdController = ThirdSubViewController()
        screenFrame.origin.x = screenFrame.size.width*(subViewCount-1)
        thirdController.view.frame = screenFrame
        
        scrollView.addSubview(firstController.view)
        scrollView.addSubview(secondController.view)
        scrollView.addSubview(thirdController.view)
        
        self.view.addSubview(scrollView)
        self.view.addSubview(pageControl)
    }
    
    @objc func pageControllDidChanged(_ sender: AnyObject){
        let crtPage = CGFloat(pageControl.currentPage)
        var frame = scrollView.frame
        frame.origin.x = frame.size.width*crtPage
        frame.origin.y = 0
        
        scrollView.scrollRectToVisible(frame, animated: true)
        isPageControlled = true
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if(!isPageControlled){
            let pageWidth = scrollView.frame.size.width
            let page = floor(scrollView.contentOffset.x-pageWidth/3)/pageWidth + 1
            pageControl.currentPage = Int(page)
        }
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        isPageControlled = false
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
